﻿using Weight.Controls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Weight.Base;

namespace Weight.Controls
{
    public class LYFSearchBox : Control
    {
        public LYFSearchBox()
        {
            this.DoubleBuffered = true;//设置本窗体
            this.SetStyle(
                      //ControlStyles.UserPaint |    //使用自定义的绘制方式
                      ControlStyles.ResizeRedraw//当控件大小发生变化时就重新绘制
                    | ControlStyles.SupportsTransparentBackColor//则控件接受 alpha 组件数小于 255 个的 BackColor 来模拟透明度
                    | ControlStyles.AllPaintingInWmPaint//则控件忽略窗口消息 WM_ERASEBKGND 以减少闪烁
                    | ControlStyles.OptimizedDoubleBuffer//则控件将首先绘制到缓冲区而不是直接绘制到屏幕，这可以减少闪烁
                    | ControlStyles.DoubleBuffer
                    , true);

            textBox = new LYFTextBox();
            textBox.MouseEnter += TextBox_MouseEnter;
            textBox.MouseLeave += TextBox_MouseLeave;
            textBox.Click += TextBox_Click;
            textBox.LostFocus += TextBox_LostFocus;
            textBox.GotFocus += TextBox_GotFocus;
            textBox.KeyDown += new System.Windows.Forms.KeyEventHandler(this.CoolTextBox_KeyDown);
            textBox.KeyUp+=new System.Windows.Forms.KeyEventHandler(this.CoolTextBox_KeyUp);
            textBox.BackColor = Color.FromArgb(255, Color.FromArgb(0x374850));
            this.Controls.Add(textBox);
            this.Size = new Size(Padding.Left + textBox.Width + textBox.Height + Padding.Right + Padding.Right, _padding.Top + textBox.Height + _padding.Bottom);

            textBox.Location = new System.Drawing.Point(_padding.Left, (this.Height - textBox.Height) / 2);
            textBox.Width = this.Width - _padding.Left * 4 - textBox.Height;
            searchRect = new Rectangle(_padding.Left + textBox.Width + _padding.Right, (this.Height - textBox.Height) / 2, textBox.Height + 4, textBox.Height + 4);
            //searchRect.Inflate(4, 4);

            borderRect.X = textBox.Location.X - _padding.Left / 2;
            borderRect.Y = textBox.Location.Y - _padding.Top / 2;
            borderRect.Width = textBox.Width + _padding.Left;
            borderRect.Height = textBox.Height + _padding.Top;
        }



        #region 属性
        [System.ComponentModel.Browsable(true)]
        [DefaultValue(typeof(Color), "SystemColors.WindowText")]
        public new Color ForeColor
        {
            get { return textBox.ForeColor; }
            set
            {
                textBox.ForeColor = value;
                this.Invalidate();
            }
        }

        private Color _backColor = SystemColors.Control;
        [System.ComponentModel.Browsable(true)]
        [DefaultValue(typeof(Color), "SystemColors.Control")]
        public new Color BackColor
        {
            get { return _backColor; }
            set
            {
                _backColor = value;
                this.Invalidate();
            }
        }

        private Padding _padding = new Padding(8);
        public new Padding Padding { get => _padding; set => _padding = value; }

        [Browsable(true)]
        [DefaultValue(typeof(String), "SystemColors.WindowText")]
        public new string Text
        {
            get { return textBox.Text; }
            set
            {
                textBox.Text = value;
                this.Invalidate();
            }
        }

        private Color _borderColor;
        [System.ComponentModel.Browsable(true)]
        [DefaultValue(typeof(Color), "System.Drawing.SystemColors.Window")]
        public Color BorderColor
        {
            get => _borderColor;
            set
            {
                _borderColor = value;
                this.Invalidate();
            }
        }


        [System.ComponentModel.Browsable(true)]
        [DefaultValue(typeof(HorizontalAlignment), "HorizontalAlignment.Left")]
        public HorizontalAlignment TextAlign { get => textBox.TextAlign; set => textBox.TextAlign = value; }
        #endregion

        #region 自定义
        public  LYFTextBox textBox;
        private Rectangle searchRect;
        private Rectangle borderRect;

        private MouseStatus mouseSearchStatus;
        private MouseStatus mouseTextBoxStatus;

        public event EventHandler<SearchClickEventArgs> SearchClick;
        #endregion

        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        private static extern IntPtr LoadLibrary(string lpFileName);

        //protected override CreateParams CreateParams
        //{
        //    get
        //    {
        //        CreateParams prams = base.CreateParams;
        //        if (LoadLibrary("msftedit.dll") != IntPtr.Zero)
        //        {
        //            prams.ExStyle |= 0x020; // transparent 
        //            prams.ClassName = "RICHEDIT50W";// TRANSTEXTBOXW
        //        }
        //        return prams;
        //    }
        //}

        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            e.Graphics.FillRectangle(new SolidBrush(_backColor), e.ClipRectangle);
            if (_backColor != Color.Transparent)
                textBox.BackColor = _backColor;
            else
            {
                Point pt = textBox.Location;
                pt.Offset(-1, -1);
                textBox.BackColor = Colors.GetScreenPointColor(this.PointToScreen(pt));
            }
            //e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(50, Color.Black)), e.ClipRectangle);



            #region 圆角矩形边框
            int cornerRadius = 3;
            GraphicsPath roundedRect = new GraphicsPath();
            //左上
            roundedRect.AddArc(borderRect.X, borderRect.Y, cornerRadius * 2, cornerRadius * 2, 180, 90);
            //上
            //roundedRect.AddLine(borderRect.X + cornerRadius, borderRect.Y, borderRect.Width - cornerRadius, borderRect.Y);
            //右上
            roundedRect.AddArc(borderRect.X + borderRect.Width - cornerRadius * 2, borderRect.Y, cornerRadius * 2, cornerRadius * 2, 270, 90);
            //右
            roundedRect.AddLine(borderRect.X + borderRect.Width, borderRect.Y + cornerRadius * 2, borderRect.Right, borderRect.Y + borderRect.Height - cornerRadius * 2);
            //右下
            roundedRect.AddArc(borderRect.Right - cornerRadius * 2, borderRect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90);
            //下
            roundedRect.AddLine(borderRect.Right - cornerRadius * 2, borderRect.Bottom, borderRect.X + cornerRadius * 2, borderRect.Bottom);
            //左下
            roundedRect.AddArc(borderRect.X, borderRect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90);
            //左
            roundedRect.AddLine(borderRect.X, borderRect.Bottom - cornerRadius * 2, borderRect.X, borderRect.Y + cornerRadius);
            roundedRect.CloseFigure();

            //边框特效
            if (mouseTextBoxStatus == MouseStatus.Enter)
                _borderColor = Color.FromArgb(255, BorderColor);
            else
                _borderColor = Color.FromArgb(50, BorderColor);
            Pen pen = new Pen(BorderColor, 1);
            e.Graphics.DrawPath(pen, roundedRect);
            #endregion

            if (mouseSearchStatus == MouseStatus.Enter)
            {
                //e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(50, Color.Black)), searchRect);
                e.Graphics.DrawImage(Properties.Resources.Search_Enter, searchRect);
            }
            else
                e.Graphics.DrawImage(Properties.Resources.Search_Normal, searchRect);
            base.OnPaint(e);
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            Point pt = new Point(e.X, e.Y);
            if (searchRect.Contains(pt))
            {
                mouseSearchStatus = MouseStatus.Enter;
                this.Cursor = Cursors.Hand;
            }
            else
            {
                mouseSearchStatus = MouseStatus.Leave;
                this.Cursor = Cursors.Default;
            }
            this.Invalidate();
            base.OnMouseMove(e);
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            if (mouseSearchStatus == MouseStatus.Enter)
                SearchClick?.Invoke(this, new SearchClickEventArgs(this.Text));
            base.OnMouseDown(e);
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);
        }


        protected override void OnKeyUp(KeyEventArgs e)
        {
            base.OnKeyUp(e);
        }
        private void CoolTextBox_KeyUp(object sender, KeyEventArgs e)
        {
            OnKeyUp(e);
        }

        private void CoolTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            OnKeyDown(e);
        }

        private void TextBox_MouseEnter(object sender, EventArgs e)
        {
            mouseTextBoxStatus = MouseStatus.Enter;
            this.Invalidate();
        }
        private void TextBox_MouseLeave(object sender, EventArgs e)
        {
            if (!textBox.Focused)
                mouseTextBoxStatus = MouseStatus.Leave;
            this.Invalidate();
        }

        private void TextBox_Click(object sender, EventArgs e)
        {
            textBox.Focus();
        }

        private void TextBox_GotFocus(object sender, EventArgs e)
        {
            mouseTextBoxStatus = MouseStatus.Enter;
            this.Invalidate();
        }

        private void TextBox_LostFocus(object sender, EventArgs e)
        {
            mouseTextBoxStatus = MouseStatus.Leave;
            this.Invalidate();
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            textBox.Location = new System.Drawing.Point(_padding.Left, (this.Height - textBox.Height) / 2);
            textBox.Width = this.Width - _padding.Left * 4 - textBox.Height;
            searchRect = new Rectangle(_padding.Left + textBox.Width + _padding.Right, (this.Height - textBox.Height) / 2, textBox.Height + 4, textBox.Height + 4);
            //searchRect.Inflate(4, 4);

            borderRect.X = textBox.Location.X - _padding.Left / 2;
            borderRect.Y = textBox.Location.Y - _padding.Top / 2;
            borderRect.Width = textBox.Width + _padding.Left;
            borderRect.Height = textBox.Height + _padding.Top;
            base.OnSizeChanged(e);
        }

        public void SetSkin(Color color)
        {
            this.BackColor = color;
        }

        private enum MouseStatus
        {
            None,
            Enter,
            Leave,
        }

        public class SearchClickEventArgs : EventArgs
        {
            public SearchClickEventArgs(string _text)
            {
                Text = _text;
            }
            public string Text { get; set; }
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // CoolSearchBox
            // 
            this.ResumeLayout(false);

        }

    }
}
